﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Util.Interface;
using Util.Interface.System;
using Util.SystemModel;
using Util.Redis;
using Util.Common;

namespace App.Controllers
{

    public class HomeController : BaseController
    {
        private readonly IUser _user;
        private readonly IMenu _menu;
        public HomeController(IUser user, IMenu menu)
        {
            _user = user;
            _menu = menu;
        }
        /// <summary>
        /// 个人信息页
        /// </summary>
        /// <returns></returns>
        public ActionResult PersonInfo()
        {
            return View();
        }
        /// <summary>
        /// 系统主界面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewBag.UserName = UserName;
            return View(_menu.GetFirstLevelMenu());
        }
        /// <summary>
        /// 用户锁定
        /// </summary>
        /// <returns></returns>
        public AjaxResult LockUser()
        {

            Redis.Set("userlock", new List<UserLockModel> { new UserLockModel() {
                userId=UserId,
                lockStatus=true
            } });
            return AjaxResult.Success();
        }
        /// <summary>
        /// 用户登录沿用以前的锁定状态
        /// </summary>
        /// <returns></returns>
        public AjaxResult CheckLockUser()
        {
            List<UserLockModel> list = Redis.Get<List<UserLockModel>>("userlock");
            if (list != null)
            {
                var temp = list.Where(O => O.userId.Equals(UserId)).FirstOrDefault();
                if (temp != null)
                {
                    return AjaxResult.Success(new
                    {
                        lockStatus = temp.lockStatus
                    });
                }
                return AjaxResult.Success(new
                {
                    lockStatus = false
                });
            }
            else
            {
                return AjaxResult.Success(new
                {
                    lockStatus = false
                });
            }
        }

        /// <summary>
        /// 用户解锁
        /// </summary>
        /// <returns></returns>
        public AjaxResult UnLockUser(string password)
        {
            password = Helper.MD5Encrypt(Helper.MD5Encrypt(password));
            var userModel = _user.UserLogin(UserName, password);
            if (userModel != null)
            {
                var list = Redis.Get<List<UserLockModel>>("userlock");
                list.RemoveAll(O => O.userId.Equals(UserId));
                Redis.Set("userlock", list);
                return AjaxResult.Success();
            }
            else
            {
                return AjaxResult.Error("密码错误，请确认后重新输入");
            }

        }
    

        public string GetMenu(string menuId)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(_menu.GetMenu(menuId));
        }
    }
}